using Core;
using Lean.Common;
using Lean.Touch;
using UnityEngine;

public class LeanTouchImple : ILeanTouchInterface
{
    private LeanConstrainToOrthographic _leanConstrainToOrthographic;
    private LeanConstrainToOrthographic leanConstrainToOrthographic
    {
        get
        {
            if (_leanConstrainToOrthographic == null)
            {
                var trans = Camera.main.transform;
                _leanConstrainToOrthographic = trans.GetComponent<LeanConstrainToOrthographic>();
            }
            return _leanConstrainToOrthographic;
        }
    }

    private LeanDragCamera _dragCamera;
    private LeanDragCamera leanDragCamera
    {
        get
        {
            if (_dragCamera == null)
            {
                var trans = Camera.main.transform;
                _dragCamera = trans.GetComponent<LeanDragCamera>();
            }
            return _dragCamera;
        }
    }

    private LeanPinchCamera _pinchCamera;
    private LeanPinchCamera leanPinchCamera
    {
        get
        {
            if (_pinchCamera == null)
            {
                var trans = Camera.main.transform;
                _pinchCamera = trans.GetComponent<LeanPinchCamera>();
            }
            return _pinchCamera;
        }
    }

    public void BindCameraBoundary(Transform terrain, Vector2 min, Vector2 max)
    {
        if (terrain == null || Camera.main == null)
        {
            return;
        }

        var leanPlane = terrain.GetComponent<LeanPlane>();

        if (leanConstrainToOrthographic == null || leanPlane == null)
        {
            return;
        }

        leanPlane.ClampX = true;
        leanPlane.MinX = min.x;
        leanPlane.MaxX = max.x;

        leanPlane.ClampY = true;
        leanPlane.MinY = min.y;
        leanPlane.MaxY = max.y;

        leanConstrainToOrthographic.Plane = leanPlane;

    }

    public void EnableDragCamera(bool enable)
    {
        if (leanDragCamera == null)
        {
            return;
        }
        leanDragCamera.enabled = enable;
    }

    public void SetPinchCameraClamp(float min, float max, float value)
    {
        if (leanPinchCamera == null)
        {
            return;
        }
        leanPinchCamera.ClampMin = min;
        leanPinchCamera.ClampMax = max;
        leanPinchCamera.Zoom = value;

    }
}
